import { addGlobalUncaughtErrorHandler, start, loadMicroApp, registerMicroApps } from "qiankun";
import { microappMap } from "./app";
import { MicroAppType } from "./type";
import permissionStore from "@/store/modules/permission";
import { RouteRecordRaw } from "vue-router";
/**
 * 添加全局的未捕获异常处理器
 */
addGlobalUncaughtErrorHandler(event => {
  console.error(event);
});
const getConfigByPath = (path: string): { config: MicroAppType.MicroAppRow; configKey: string } => {
  for (const key in microappMap) {
    const reg = RegExp(`^(${key})`);
    if (reg.test(path))
      return {
        config: microappMap[key],
        configKey: key
      };
  }
  return {
    config: {} as MicroAppType.MicroAppRow,
    configKey: ""
  };
};
//筛选出指定子应用的路由列表
const getMicroAppRouteList = (key: string) => {
  const result = permissionStore.asyncRoutes.find((t: RouteRecordRaw) => t.path === key) || {};
  return result.children || [];
};
// 筛选出指定子应用的路由缓存组件name
const getMicroAppRouteCache = (key: string) => {
  const result = getMicroAppRouteList(key);
  const keepAliveList = result.filter((t: RouteRecordRaw) => t.meta?.keepAlive);
  return keepAliveList.map((t: RouteRecordRaw) => t.name);
};
export { loadMicroApp, registerMicroApps, start, getConfigByPath, getMicroAppRouteCache, getMicroAppRouteList };
